#!/bin/sh
date=`date +%Y%m%d%m%s`
shortdate=`echo ${date} | sed s/^...//`
shortname=text-packet-${shortdate}.txt
USERNAME=`whoami` ; T1="root"
if [ "$T1" = "$USERNAME" ]; then 
echo  'Please do not run this script as root' ; exit
fi

# Declaration of allowed symbol for user input scrubbing
declare -r AllowedChars="rusengfradeuspaitanldvie"


if [ ! -z "$2" ]; then
echo "Use 1 argument only. Usage: ocr language. Sample ocr eng, ocr rus, ocr fra, ocr deu, ocr spa, ocr ita, ocr nld, ocr vie" ; exit
fi

if [ ! -z "$1" ]
then
VAR_01=$1
else
echo "To start ocr you need to type language as argument. Usage: ocr eng, ocr rus" ; exit
fi

echo ""
if [ -e /home/"$USERNAME"/ocr  ]; then
echo  " "
else
mkdir -p /home/$USERNAME/ocr
echo  "Creating /home/"$USERNAME"/ocr directory"
mkdir -p /home/$USERNAME/ocr/rus
echo  "Creating /home/"$USERNAME"/ocr/rus directory for Russian Language images"
mkdir -p /home/$USERNAME/ocr/eng
echo  "Creating /home/"$USERNAME"/ocr/eng directory for English language images"
mkdir -p /home/$USERNAME/ocr/fra
echo  "Creating /home/"$USERNAME"/ocr/fra directory for Franch language images"
mkdir -p /home/$USERNAME/ocr/deu
echo  "Creating /home/"$USERNAME"/ocr/deu directory for German language images" 
mkdir -p /home/$USERNAME/ocr/spa
echo  "Creating /home/"$USERNAME"/ocr/spa directory for Spanish language images" 
mkdir -p /home/$USERNAME/ocr/ita
echo  "Creating /home/"$USERNAME"/ocr/ita directory for Italian language images"
mkdir -p /home/$USERNAME/ocr/nld
echo  "Creating /home/"$USERNAME"/ocr/nld directory for Dutch language images"
mkdir -p /home/$USERNAME/ocr/vie
echo  "Creating /home/"$USERNAME"/ocr/vie directory for Vienamian language images"
mkdir -p /home/$USERNAME/ocr/txt
echo  "Creating /home/"$USERNAME"/ocr/txt directory for output text "

echo '------------------------------------------------------------'
echo  "Place your images to /home/"$USERNAME"/ocr/your_lang directory and then rerun this script" 
exit
fi

# Checking user input&scrubbing
ScrubbedCheck="${1//[^$AllowedChars]/}"
if [ "$1" = "$ScrubbedCheck" ]; then
echo  ''
else
echo ' '
echo " Error. You entered wrong symbols. Allowed symbols are: "$AllowedChars""
echo 'Run this script again and be more carefull during inputing.'
exit
fi

VAR_01=$1

f1()

{

echo ''
echo '-----------------------------------------------------'
echo "Begin OCR in /home/$USERNAME/ocr/"$VAR_01"/          "
echo '-----------------------------------------------------'
echo ''

if [ "$VAR_01" = "rus" ]; then 
echo  'Language of OCR is Russian'
fi

if [ "$VAR_01" = "eng" ]; then 
echo  'Language of OCR is English'
fi

if [ "$VAR_01" = "fra" ]; then 
echo  'Language of OCR is French'
fi

if [ "$VAR_01" = "deu" ]; then 
echo  'Language of OCR is German'
fi

if [ "$VAR_01" = "spa" ]; then 
echo  'Language of OCR is Spanish'
fi

if [ "$VAR_01" = "ita" ]; then 
echo  'Language of OCR is Italian'
fi

if [ "$VAR_01" = "nld" ]; then 
echo  'Language of OCR is Dutch'
fi

if [ "$VAR_01" = "vie" ]; then 
echo  'Language of OCR is Vietnamian'
fi

echo '------------------------------------------------------------'
echo ''

cd /home/$USERNAME/ocr/$VAR_01
ls -A1 > /tmp/filelist
cd /home/$USERNAME/ocr/txt/

if [ -e "$shortname"  ]; then
rm $shortname
fi

for file in $(cat /tmp/filelist); do
echo "File "$file" proccesing by "
tesseract ../$VAR_01/$file $file -l $VAR_01
echo "Done! Text saved to /home/"$USERNAME"/ocr/txt/"$file".txt file."
cat "$file".txt >> $shortname  
echo '-------------------------------------------------------------'
echo ''
done

echo '-------------------------------------------------------------'
echo '-------------------------------------------------------------'
echo "OCR is done! Check *.txt file in /home/"$USERNAME"/ocr/txt/"
echo "All OCR text files saved to "$shortname" file."
echo '-------------------------------------------------------------'

} 
f1 "${VAR_01}"
